package com.pojoin.cms.util.pager;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.pojoin.cms.context.MessageConst;


@SuppressWarnings("serial")
public class PagerTag extends TagSupport {
	private MessageConst messageConst = new MessageConst();
	private int pageNo = 1;
	private int pageSize = 10;
	private int resultCount;
	private String searchkey;
	private String searchvalue;

	public String getSearchkey() {
		return searchkey;
	}

	public void setSearchkey(String searchkey) {
		this.searchkey = searchkey;
	}

	public String getSearchvalue() {
		return searchvalue;
	}

	public void setSearchvalue(String searchvalue) {
		this.searchvalue = searchvalue;
	}

	public int getResultCount() {
		return resultCount;
	}

	public void setResultCount(int resultCount) {
		this.resultCount = resultCount;
	}

	private String action;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	@Override
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter writer = this.pageContext.getOut();
		try {
			writer.write(this.getTagString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	private String getTagString() {
		int start = 1;
		int end = this.getPageCount();
		if (end > 7) {
			if (this.pageNo > 4 && this.pageNo < this.getPageCount() - 3) {
				start = this.pageNo - 3;
				end = this.pageNo + 3;
			}
			if (this.pageNo > 4 && this.pageNo >= this.getPageCount() - 3) {
				start = this.getPageCount() - 6;
				end = this.getPageCount();
			}
			if (this.pageNo <= 4 && this.pageNo < this.getPageCount() - 3) {
				start = 1;
				end = 7;
			}
		}
		StringBuffer sb = new StringBuffer("");
		String script = "<script language=\"JavaScript\"> "
				+ "function sel() { "
				+ "location.href=document.myform.menu1.value; " + "} "
				+ "</script>\n";
		sb.append(script);
		sb.append("<div>\n<table class=\"zi\">\n<tr>\n<td valign=\"top\">\n"
				+ this.messageConst.getValueByKey("pagertag.resultcount")
				+ this.resultCount + "&nbsp;&nbsp;"
				+ this.messageConst.getValueByKey("pagertag.pagecount")
				+ this.getPageCount() + "&nbsp;&nbsp;\n");
		if (this.pageNo <= 1) {
			sb.append(this.messageConst.getValueByKey("pagertag.shouye")
					+ "&nbsp;&nbsp;");
		} else {
			sb.append("<a href=\"" + this.action + "?pager.pageNo=1&"
					+ this.searchkey + "=" + this.searchvalue + "\" >"
					+ this.messageConst.getValueByKey("pagertag.shouye")
					+ "&nbsp;&nbsp;" + "</a>\n");
		}
		if (this.getPrevious()) {
			sb.append("<a href=\"" + this.action + "?pager.pageNo="
					+ (this.pageNo - 1) + "&" + this.searchkey + "="
					+ this.searchvalue + "\" >"
					+ this.messageConst.getValueByKey("pagertag.shangyiye")
					+ "&nbsp;&nbsp;" + "</a>\n");
		} else {
			sb.append(this.messageConst.getValueByKey("pagertag.shangyiye")
					+ "&nbsp;&nbsp;");
		}
		if (start > 1) {
			sb.append("...");
		} else {
			sb.append("&nbsp;&nbsp;&nbsp;");
		}
		for (int i = start; i <= end; i++) {
			if (this.pageNo != i) {
				sb.append("<a href=\"" + this.action + "?pager.pageNo=" + i
						+ "&" + this.searchkey + "=" + this.searchvalue
						+ "\" >" + i + "&nbsp;" + "</a>\n");
			} else {
				sb.append("<font size=\"3\" color=\"red\"><b>" + i + "&nbsp;"
						+ "</b></font>");
			}
		}
		if (end < this.getPageCount()) {
			sb.append("...");
		} else {
			sb.append("&nbsp;&nbsp;&nbsp;");
		}
		if (this.getNext()) {
			sb.append("<a href=\"" + this.action + "?pager.pageNo="
					+ (this.pageNo + 1) + "&" + this.searchkey + "="
					+ this.searchvalue + "\" >"
					+ this.messageConst.getValueByKey("pagertag.xiayiye")
					+ "&nbsp;&nbsp;" + "</a>\n");
		} else {
			sb.append(this.messageConst.getValueByKey("pagertag.xiayiye")
					+ "&nbsp;&nbsp;");
		}
		if (this.pageNo >= this.getPageCount()) {
			sb.append(this.messageConst.getValueByKey("pagertag.weiye")
					+ "&nbsp;&nbsp;");
		} else {
			sb.append("<a href=\"" + this.action + "?pager.pageNo="
					+ this.getPageCount() + "&" + this.searchkey + "=\""
					+ this.searchvalue + "\" >"
					+ this.messageConst.getValueByKey("pagertag.weiye")
					+ "&nbsp;&nbsp;" + "</a>\n");
		}
		sb
				.append("</td>\n<td valign=\"center\">\n<form name=\"myform\">\n<select name=\"menu1\" onChange=\"sel()\">\n");
		for (int i = 1; i <= this.getPageCount(); i++) {
			sb.append("<option value=\"" + this.action + "?pager.pageNo=" + i
					+ "&" + this.searchkey + "=" + this.searchvalue + "\"");
			if (this.pageNo == i) {
				sb.append("selected=\"true\"");
			}
			sb.append(">" + this.messageConst.getValueByKey("pagertag.di") + i
					+ this.messageConst.getValueByKey("pagertag.ye")
					+ "</option>\n");
		}
		sb.append("</select>\n</form>\n</td>\n</tr>\n</table>\n");
		sb.append("</div>");
		return sb.toString();
	}

	private boolean getNext() {
		if (this.pageNo >= this.getPageCount()) {
			return false;
		}
		return true;
	}

	private boolean getPrevious() {
		if (this.pageNo <= 1) {
			return false;
		}
		return true;
	}

	private int getPageCount() {
		int pageCount = 0;
		if (this.resultCount % this.pageSize == 0) {
			pageCount = this.resultCount / this.pageSize;
		} else {
			pageCount = this.resultCount / this.pageSize + 1;
		}
		return pageCount;
	}

	public static void main(String[] a) {
		PagerTag p = new PagerTag();
		p.resultCount = 111;
		p.pageNo = 5;
		System.out.println(p.getTagString());
	}
}
